from django.contrib.contenttypes.models import ContentType
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django.contrib.auth.models import Permission, Group

from meiduo_admin.serializers.permission import PermissionSerializer, ContentTypeSerializer, GroupSerializer, \
    PermSimpleSerializer, AdminSerializer, GroupSimpleSerializer

# ================权限数据管理===================
from users.models import User


class PermissionViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer
    lookup_value_regex = '\d+'

    # GET /meiduo_admin/permission/perms/  ====>获取权限表列表数据 ---lsit
    # GET /meiduo_admin/permission/perms/ ----create
    # GET permission/permission/perms/(?P<pk>\d+)/$  --retrieve
    # PUT permission/permission/perms/(?P<pk>\d+)/$  --update
    # delete permission/permission/perms/(?P<pk>\d+)/$  --destroy

    def content_types(self, request):
        # GET /meiduo_admin/permission/content_types/  ---content_types
        # 获取权限类型数据
        # 1.查询数据库获取权限类型数据
        # 2.将权限类型的数据序列化并返回
        c_types = ContentType.objects.all()
        serializer = ContentTypeSerializer(c_types, many=True)
        return Response(serializer.data)


# ================用户组数据管理===================
class GroupViewSet(ModelViewSet):
    '''
    获取用户组表列表数据:GET /meiduo_admin/permission/groups/---list
    保存用户组数据 POST /meiduo_admin/permission/groups/ ---create
    获取权限简单数据: GET /meiduo_admin/permission/simple/----simple
    修改指定的用户组数据: PUT /meiduo_admin/permission/groups/(?P<pk>\d+)/ --update
    删除用户组表数据: DELETE /meiduo_admin/permission/groups/(?P<pk>\d+)/-destory
    '''
    permission_classes = [IsAdminUser]
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    lookup_value_regex = '\d+'

    def simple(self, request):
        '''
        获取权限简单数据: GET /meiduo_admin/permission/simple/
        1.查询数据库,获取所有权限数据
        2.序列化返回
        '''
        perms = Permission.objects.all()
        serializer = PermSimpleSerializer(perms, many=True)
        return Response(serializer.data)


# ================管理员用户管理===================
class AdminViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    '''
    获取管理员用户列表数据:GET /meiduo_admin/permission/admins/ ---list
    保存管理员数据: POST /meiduo_admin/permission/admins/---create
    获取指定的管理员数据 GET /meiduo_admin/permission/admins/(?P<pk>\d+)/
    修改指定的管理员数据 PUT /meiduo_admin/permission/admins/(?P<pk>\d+)/
    删除管理员数据 DELETE /meiduo_admin/permission/admins/(?P<pk>\d+)/
    '''
    queryset = User.objects.filter(is_staff=True)
    serializer_class = AdminSerializer
    lookup_value_regex = '\d+'

    def simple(self, request):
        '''
        获取用户组简单数据:GET /meiduo_admin/permission/groups/simple/
        1.查询数据库,获取所有用户组的数据库
        2.序列化返回
        '''
        groups = Group.objects.all()
        serialzier = GroupSimpleSerializer(groups, many=True)
        return Response(serialzier.data)
